package com.viewhigh.wechat.sdk.api;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;
import org.sword.lang.HttpUtils;

import com.alibaba.fastjson.JSONObject;
import com.viewhigh.framework.common.log.LogUtils;
import com.viewhigh.wechat.sdk.base.BaseApi;
import com.viewhigh.wechat.sdk.token.RqSceneTicket;

/**
 * @Title: QrSceneUtil.java
 * @Package com.viewhigh.wechat.sdk.util
 * @Description: 带参数二维码工具类
 * @author liusongqing
 * @Company www.viewhigh.com
 * @date 2016年4月29日 下午1:58:49
 * @version V1.0
 */
public class QrSceneApi extends BaseApi{
    /**
     * 临时二维码 http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式：json
     * POST数据例子：{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
     */
    private static final String GET_TEMP_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
    /**
     * 永久二维码 http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式：json
     * POST数据例子：{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
     * 或者也可以使用以下POST数据创建字符串形式的二维码参数： {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str":
     * "123"}}}
     */
    private static final String GET_LIMIT_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
    /**
     * 获取二维码
     */
    private static final String GET_QRCODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";

    /**
     * 
    * @Title: createTempTicket 
    * @Description: 获取临时二维码 
    * @param @param sceneId 整型参数
    * @param @param appId   
    * @param @return    设定文件 
    * @return RqSceneTicket    返回类型 
    * @throws
     */
    public static RqSceneTicket createTempTicket(String sceneId,String appId) {
        RqSceneTicket ticket = new RqSceneTicket();
        // 构造请求数据
        String url = GET_TEMP_TICKET_URL + getAccessTokenStr(appId);
        JSONObject json = new JSONObject();
        json.put("expire_seconds", "2592000");
        json.put("action_name", "QR_SCENE");
        JSONObject scene = new JSONObject();
        JSONObject actionInfo = new JSONObject();
        scene.put("scene_id", sceneId);
        actionInfo.put("scene", scene);
        json.put("action_info", actionInfo);
        String data = json.toJSONString();

        String result = HttpUtils.post(url, data);
        LogUtils.debugLog("QrSceneApi createTempTicket post result:" + result);
        // 解析请求数据
        JSONObject resultJson = JSONObject.parseObject(result);
        if (null != resultJson) {
            ticket.setExpire_seconds("expire_seconds");
            ticket.setTicket(resultJson.getString("ticket"));
            ticket.setUrl(resultJson.getString("url"));
        } else {
            LogUtils.error("QrSceneApi get template id error");
        }
        return ticket;
    }

    public static RqSceneTicket createLimitTicket(String scene_str,String appId) {
        RqSceneTicket ticket = new RqSceneTicket();
        // 构造请求数据
        String url = GET_LIMIT_TICKET_URL + getAccessTokenStr(appId);
        JSONObject json = new JSONObject();
        json.put("action_name", "QR_LIMIT_STR_SCENE");
        JSONObject scene = new JSONObject();
        JSONObject actionInfo = new JSONObject();
        scene.put("scene_str", scene_str);
        actionInfo.put("scene", scene);
        json.put("action_info", actionInfo);
        String data = json.toJSONString();

        String result = HttpUtils.post(url, data);
        // 解析请求数据
        JSONObject resultJson = JSONObject.parseObject(result);
        if (null != resultJson && StringUtils.isNotBlank(resultJson.getString("ticket"))) {
            ticket.setTicket(resultJson.getString("ticket"));
            ticket.setUrl(resultJson.getString("url"));
        } else {
            String error = "create limit ticket error ";
            if (StringUtils.isNotBlank(resultJson.getString("errmsg"))) {
                error += resultJson.getString("errmsg");
            }
            LogUtils.error(error);
        }
        return ticket;
    }
    
    /**
     * 
     * @Title: getQrcodeUrl @Description: 获取二维码地址 @param @param ticketStr @param @return 设定文件 @return String
     * 返回类型 @throws
     */
    @SuppressWarnings("finally")
    public static String getQrcodeUrl(String ticketStr) {
        try {
            ticketStr = java.net.URLEncoder.encode(ticketStr, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            LogUtils.error("getQrcodeUrl error:" + e.getMessage());
        } finally {
            return GET_QRCODE_URL + ticketStr;
        }

    }

}
